Cleaned up layer API and fixed some bugs