Cleanup/optimize code and API