Various bugs fixing