beautified Jacco's code