refactored main code, much cleaner and easier to read now