WIP: refactor and stable version