added the draw mask function, and more error checking