moved some code to make things more readable/maintainable