rewrote functions and structures, not stable