fixed errors and made it more performant