Cleanup and code improvement