Fix bugs introduced by last commit and finish issue #2