Reordered code and added clipping to map