Switched to some NumPy functions for faster performance