reimplementation; bug fixes