Fixed small bug in function