Complete revision, few bug fixes