refactored some old ugly code