Some refactoring and added some documentation