Refactor code to follow more best practices