Refactor code to more maintainable structure