Fixed compass error. Encapsulated main function code.