Add dynamic array and fix bugs