Fine tunning, some refactor, tiny bugs fixing