fixing up buggy code