fixed stackoverflow on close by flushing, added after and before back

some unittests not working yet