Lots of refactoring done