Code from playframework source