Refactoring and writing some docs