rewrite source code

improve source code for easy reading.
shorten insert/update active record.