Cleaning up getN algorithm