Fixes and refactoring #20, #21, #22, #23