added new functionality to pm function and cleaned up a little