Ugly/hacky rewrite of js to get legends and grid layers working