Refactored a bunch of code