added assert statements, cleaned up code