Did some refactoring, moved some code around