refactored code and removed unneeded parts of main