some refactoring. Moved function definitions to main.h