Fixed few bugs, properly tested