Most of the refactoring done