Cleaned up and verified correctness