added P_IN function, made some changes with P_OUT to improve speed