refactored the code and cleaned  it up