refactored main to work properly