cleaned up code, added more comments