Refactored important code into reusable bits