function written with bugs