refactored some code, reduced LoC