simplified function quite a bit