Refactorize some code to make it simpler

Hello @nicks, @giannic, 

Please review the following commits I made in branch 'xiao-refactorize'.

16fcdafa64484e5835be743b8ec706f36751df93 (2014-03-25 15:38:34 -0700)
Refactorize some code to make it simpler

R=@nicks
R=@giannic