Adding support for layer packing

- Only keep state for the scan code that are available for the layer
- Also properly implemented scan code range checking (was not done before, only worked because the KLL compiler was well behaved)