More code cleanup and api improvements