refactored main code