solved problem by rewriting code