added singleton template, fixed some depth bugs