moved the actual routine into a seperate source from the main. This allows the routine to be used in other applications.