cleaned up, removed test code, better documentation/comments, minor optimizations