Improve API and clean up code