Major code refactoring, cleaned code up