tranlated prav's new, working code into the main file