#18 Additional fixes and refactoring