some cleanup refactoring; still need comments and tests