Did a bit of refactoring and added some documentation