Cleaning up warnings and partially refactoring