refactoring and various improvements after code review, more to come