Misc fixes at various places

Do things with less code.
Treat OOM case in various places.
Treat return values.