Cleanup code and clarify documentation