Merged many things from homeslice's source