added criteria parameter, cleaned up some code