added open-close functionality, will clean up later
another little fix
some heavy refactoring