fixed listening bug and cleaned up code