Corrected potential bug and added unit test