initial reimplementation
large revision
moved declaration