preliminary hurried commit. Will possibly roll this back later to commit...

... with better structure and comments