Fixed compilation and logical errors