Add validation for REST API, cleanup some code