refactor code for readability and saner formatting