Added comments and cleaned up some code. Current version considered stable.