Cleaned up the logic a bit in the API