Refactoring code for more sanity