Various fixes & code cleanup