layout replacement note: code is really sloppy, will be optimized