Fixed bug in helper function