Started refactoring the code a little