Fix some warnings and do some optimizations