fixed end and flush behavior and added tests