fixed errors and sped up code