added test, comments. cleaned up code a bit