clean up messy function