cleaner way of doing this using function