remove duplicate code and fixing return