Working generalised version
Fixed initial load bug