Many bugfixes

Change-Id: I9a443effe989545538884e0587fd5e5ce61a32c7
Signed-off-by: Jeremy Ho <jujaga@gmail.com>