fixed problems with restrictions and reformatted many related code.