Refactor/fix bundling and write bundle to file