fixed a bug and made a function