improve logic as well as handling of inputs