Cleaned up the procs a bit and added safety checks