improved speed and fixed code errors