rearranged the code and removed some useless lines