cleaned up code, starting rewrite