Refactoring, fixes and improvements