Fixed memory checking functions (problem with cast, error in checking stack bounds as well)