Refactor code to make it cleaner