refactoring almost complete. ready to merge master