Bug fixing and general improvement