Cleaned up galaxy masking and count code