Refactored lots of code