refactored some code to clean it a bit