added more docs, cleaned up code