fixed compiler bugs and added refrence checks in templates fixed misc bugs