Rewrote __getitem__ to be more readable and reduce code duplication.