New logic and some bug fixes