refactored some ugly parts