Fixed bug in function