Modified from appframework